import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

print('使用selenium模拟登陆')
# 使用selenium模拟登陆，获取并返回cookie
username = '13711154954'
password = '1835584027baby'
options = webdriver.ChromeOptions()  # 声明浏览器的对象
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--disable-blink-features')
options.add_argument('--disable-blink-features=AutomationControlled')  # 去除浏览器selenium监控
# options.add_argument('--headless')  # 浏览器不提供可视化页面
options.add_argument('--disable-gpu')  # 禁用GPU加速
browser = webdriver.Chrome(options=options)
browser.get('https://login.1688.com/member/signin.htm')
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-id"]').send_keys(username)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-password"]').send_keys(password)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@type="submit"]').click()
time.sleep(4)
# 检查是否出现了滑动验证码
try:
    slider = browser.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
    ActionChains(browser).click_and_hold(slider).perform()  # 按住滑块
    ActionChains(browser).move_by_offset(300, 0).perform()  # 移动到最右边
    ActionChains(browser).pause(0.5).release().perform()  # 松开鼠标
except:
    pass
# cookies_dict = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}
# driver.quit()
# print(cookies_dict)